package com.xuecheng.manage_cms.service;


import com.xuecheng.framework.domain.cms.CmsTemplate;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.dao.CmsTemplateRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 模板管理实现层
 */
@Service
public class CmsTemplateService {


    @Autowired
    private CmsTemplateRepository cmsTemplateRepository;

    /**
     * 模板分页查询接口
     * @param page
     * @param size
     * @param cmsTemplate
     * @return
     */
    public QueryResponseResult findList(int page, int size, CmsTemplate cmsTemplate){
        //条件匹配器,
        ExampleMatcher exampleMatcher =ExampleMatcher.matching();
        //创建模糊条件查询
        exampleMatcher =exampleMatcher.withMatcher("templateName",ExampleMatcher.GenericPropertyMatchers.contains());
        //创建条件实例，有意思的是实体类字段有值的时候自动默认是精确查询，所以有时候可能要过滤一下
        Example<CmsTemplate> example= Example.of(cmsTemplate,exampleMatcher);
        Pageable pageable= PageRequest.of(page,size);

        Page<CmsTemplate> list= cmsTemplateRepository.findAll(example,pageable);
        QueryResult<CmsTemplate> queryResult=new QueryResult<CmsTemplate>();
        queryResult.setList(list.getContent());
        queryResult.setTotal(list.getTotalElements());
        queryResult.setTotalPage(list.getTotalPages());
        return new QueryResponseResult(CommonCode.SUCCESS,queryResult);

    }

    /**
     * 查询所有
     * @return
     */
    public ResponseResult<List<CmsTemplate>> findAllList(){
        List<CmsTemplate> list= cmsTemplateRepository.findAll();
        return new ResponseResult(CommonCode.SUCCESS,list);
    }

    /**
     * 模板新增接口
     * @param cmsTemplate
     * @return
     */
    public ResponseResult<CmsTemplate> add(CmsTemplate cmsTemplate){

        cmsTemplate.setTemplateId(null);
        cmsTemplate.setTemplateCreateTime(new Date());
        cmsTemplateRepository.save(cmsTemplate);
        return ResponseResult.SUCCESS(cmsTemplate);
    }


    /**
     * 根据id查询模板
     * @param id
     * @return
     */
    public ResponseResult<CmsTemplate> findById(String id){
        Optional<CmsTemplate> optional = cmsTemplateRepository.findById(id);
        return new ResponseResult(CommonCode.SUCCESS,optional.get());
    }

    /**
     * 更新模板信息
     * @param cmsTemplate
     * @return
     */
    public ResponseResult<CmsTemplate> update(CmsTemplate cmsTemplate) {
        //根据id查询模板信息
        CmsTemplate one = cmsTemplateRepository.findById(cmsTemplate.getTemplateId()).get();
        if (one != null) {
            cmsTemplate.setTemplateCreateTime(one.getTemplateCreateTime());
            //执行更新
            CmsTemplate save = cmsTemplateRepository.save(cmsTemplate);
            if (save != null) {
                //返回成功
                return ResponseResult.SUCCESS(save);
            }
        }
        //返回失败
        return new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 模板删除接口
     * @param id
     * @return
     */
    public ResponseResult delete(String  id){
        if(id!=null){
            cmsTemplateRepository.deleteById(id);
            return  new ResponseResult(CommonCode.SUCCESS);
        }
        return  new ResponseResult(CommonCode.FAIL);
    }
}